package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class AddressController {
    @Autowired
    AddressService addressService;

    //查询所有地址
    @GetMapping("/address/list")
    public ResultInfo list(){
        List<Address> addressList = addressService.list();
        return ResultInfo.success(addressList);
    }

    //添加地址
    @PostMapping("/address")
    public ResultInfo add(@RequestBody Address address){
        addressService.add(address);
        return ResultInfo.success();
    }

    //修改默认地址的状态(0,1)
    @PutMapping("/address/default")
    public ResultInfo update(@RequestBody Address address){
        addressService.update(address);
        return ResultInfo.success();
    }

    //查询默认地址
    @GetMapping("/address/default")
    public ResultInfo findDefault(){
        Address address = addressService.findDefault();
        return ResultInfo.success(address);
    }

    //回显地址
    @GetMapping("/address/{id}")
    public ResultInfo findById(@PathVariable Long id){
        Address address = addressService.findById(id);
        return ResultInfo.success(address);
    }

    //修改地址
    @PutMapping("/address")
    public ResultInfo updateAddress(@RequestBody Address address){
        addressService.updateAddress(address);
        return ResultInfo.success();
    }
}
